<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>迭代器协议</title>
    <script src="js/traceur.js"></script>
    <script src="js/bootstrap.js"></script>
    <script type="module">

        //迭代器协议
        let obj = {
            array: [1, 2, 3, 4, 5],
            nextIndex: 0,
            next: function () {
                return this.nextIndex < this.array.length ?
                    {value: this.array[this.nextIndex++], done: false} : {done: true};
            }
        };
        console.log(obj.next().value);  //1
        console.log(obj.next().value);  //2
        console.log(obj.next().value);  //3
        console.log(obj.next().value);  //4
        console.log(obj.next().value);  //5
        console.log(obj.next().done);  //true

        let obj1 = {
            array: [1, 2, 3, 4, 5],
            nextIndex: 0,
            [Symbol.iterator]: function(){
                return {
                    array: this.array,
                    nextIndex: this.nextIndex,
                    next: function(){
                        return this.nextIndex < this.array.length ?
                            {value: this.array[this.nextIndex++], done: false} :
                            {done: true};
                    }
                }
            }
        };
        let iterable = obj1[Symbol.iterator]();
        console.log(iterable.next().value); // 1
        console.log(iterable.next().value); // 2
        console.log(iterable.next().value); // 3
        console.log(iterable.next().value); // 4
        console.log(iterable.next().value); // 5
        console.log(iterable.next().done); //true
    </script>
</head>
<body>

</body>
</html>